package com.gopay.bis.web.controller.ebank;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.GdbConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.gdb.ebank.GdbB2cMessage;

/**
 * 广发银行B2C网关支付结果
 * 
 * @GdbB2cServerRes.java
 * @author zhengxue
 * @2014-11-13 上午11:38:40 www.gopay.com.cn Inc.All rights reserved.
 */
@Controller
@Scope("prototype")
public class GdbB2cServerRes extends BankResBase {

    private static Log logger = LogFactory.getLog(GdbB2cServerRes.class);

    @RequestMapping(value = "/gdbB2cServerRes")
    protected ModelAndView execute(HttpServletRequest request, HttpServletResponse response) {
        logger.error("--------------- GdbB2cServerRes bankback ---------------");
        return super.execute(request, response);
    }

    @Override
    protected BaseMessage buildMessage(HttpServletRequest request) {
        GdbB2cMessage gdbMessage = new GdbB2cMessage();
        gdbMessage.setBank(BankConstants.BANK_LOGO_GDB);
        String amt = request.getParameter("amount");
        String orderid = request.getParameter("orderid");
        String sequence = request.getParameter("sequence");
        String orderdate = request.getParameter("orderdate");
        String retcode = request.getParameter("success");
        String merchid = request.getParameter("merchid");
        String crypt = request.getParameter("crypt");

        gdbMessage.setSignMessage(crypt);
        gdbMessage.setBankOrderId(orderid);
        gdbMessage.setAmount(new BigDecimal(amt));
        gdbMessage.setRetCode(retcode);
        gdbMessage.setMerchId(merchid);
        gdbMessage.setOrderDate(orderdate);
        
        String plainMessage = buildPlainB2C(gdbMessage,sequence);
        gdbMessage.setPlainMessage(plainMessage);
        // 银行业务产品00个人网银支付
        gdbMessage.setBankBusProd(BisBankBusinessProd._00);
        return gdbMessage;

    }
    
    /**
	 * 构建B2C报文
	 * 
	 * @param message
	 *            广发银行B2C数据结构体
	 * @return String 商户验签源数据
	 */
	public String buildPlainB2C(GdbB2cMessage message,String sequence) {
		StringBuffer sb = new StringBuffer("");
		sb.append(message.getBankOrderId()).append("||")
		.append(message.getAmount()).append("||")
		.append(message.getOrderDate()).append("||")
		.append(message.getRetCode()).append("||")
		.append(sequence).append("||")
		.append(message.getMerchId());
		return sb.toString();
	}

    @Override
    protected OrderInfo buildOrderInfo(HttpServletRequest request, Result result) {
        OrderInfo orderInfo = new OrderInfo();
        GdbB2cMessage gdbMessage = (GdbB2cMessage) result.getModel("message");
        if (gdbMessage != null) {
            if (GdbConstants.PAY_STATUS_SUCCESS.equals(gdbMessage.getRetCode())) {
                orderInfo.setOrderId(gdbMessage.getOrderId());
                orderInfo.setAmt(gdbMessage.getAmount().toString());
                orderInfo.setNotifyNum("1");
                orderInfo.setPayStatus("00");
                orderInfo.setRetCode("00");
                orderInfo.setRetMsg("");
            } else {
                orderInfo.setOrderId(gdbMessage.getOrderId());
                orderInfo.setAmt(gdbMessage.getAmount().toString());
                orderInfo.setNotifyNum("1");
                orderInfo.setPayStatus("01");
                orderInfo.setRetCode("01");
                orderInfo.setRetMsg("");
            }
        }
        return orderInfo;
    }

    @Override
    protected boolean checkParameters(HttpServletRequest request) {
        if (StringUtils.isEmpty(request.getParameter("orderid")) || StringUtils.isEmpty(request.getParameter("crypt"))) {
            return false;
        }
        return true;
    }

}
